<?php
/**
 * User: hjd-yao
 * Date: 2018/12/14
 * Time: 11:13
 */

namespace Develop\Http\Controllers;


use App\Helpers\Search;
use App\Http\Controllers\Controller;
use App\Helpers\SelfResponse;
use App\Http\Requests\ConfigRequest;
use Develop\Models\ConfigModel;
use Illuminate\Http\Request;

class ConfigController extends Controller
{
    public function __construct()
    {
        view()->share('config_group',config('app.conf_group_list'));
        view()->share('config_type',config('app.conf_type_list'));
    }

    /**
     * @title配置管理
     * @param $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $search = Search::make($request)->bindModel(new ConfigModel())
                    ->input('name','标识','',['symbol'=>'like'])->input('title','标题')
                    ->time('created_at','创建时间')
                    ->select('group','分组',format_option(config('app.conf_group_list')))
                    ->select('type','类型',format_option(config('app.conf_type_list')))
                    ->bindSearchData();

        $list = $search->where()->orderBy('type','asc')->paginate(20);

        return view('develop::config.index',[
            'page_title'=>'配置列表',
            'list'=>$list,
            'search_tpl'=>$search
        ]);
    }

    /**
     * @title添加配置
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showAddForm()
    {
        return view('develop::config.add_or_edit',[
            'page_title'=>'添加配置'
        ]);
    }

    /**
     * @title保存新增配置
     * @param ConfigRequest $request
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function save(ConfigRequest $request)
    {
        $data = $request->post();
        if(ConfigModel::init()->saveConfig($data)){
            return SelfResponse::success('添加成功','b_Config_index');
        }
        return SelfResponse::error('添加配置失败');
    }

    /**
     * @title 编辑配置
     * @param $name
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function showEditForm($name)
    {
        $info = ConfigModel::where('name',$name)->first();
        if(!$info){
            return redirect()->route('b_Index_index');
        }
        return view('develop::config.add_or_edit',[
            'page_title'=>'编辑'.$info->title,
            'config_info'=>$info,
        ]);
    }

    /**
     * @title 更新配置
     * @param ConfigRequest $request
     * @param $name
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function update(ConfigRequest $request,$name)
    {
        $info = ConfigModel::where('name',$name)->first();
        if(!$info){
            return SelfResponse::error('编辑的配置信息不存在');
        }
        $data = $request->post();
        if(ConfigModel::init()->updateConfig($name,$data)){
            return SelfResponse::success('编辑'.$info->title.'配置成功','b_Config_index');
        }
        return SelfResponse::error('编辑'.$info->title.'配置失败!');
    }

    /**
     * @title 删除配置
     * @param $name
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function delete($name)
    {
        $info = ConfigModel::where('name',$name)->first();
        if(!$info){
            return SelfResponse::error('删除的信息不存在');
        }
        if(ConfigModel::init()->deleteConfig($name)){
            return SelfResponse::success('成功删除'.$info->title,'b_Config_index');
        }
        return SelfResponse::error($info->title.'删除失败');
    }

    /**
     * @title 启用/禁用配置
     * @param $name
     * @return SelfResponse|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function switchConfig($name)
    {
        $info = ConfigModel::where('name',$name)->first();
        if(!$info){
            return SelfResponse::error('操作的信息不存在');
        }
        $status = abs($info->status-1);
        $msg = $status == 1?'启用':'禁用';
        if(ConfigModel::init()->changeStatus($name,$status)){
            return SelfResponse::success('成功'.$msg.$info->title,'b_Config_index');
        }
        return SelfResponse::error($info->title.$msg.'失败');
    }

    /**
     * @title 批量删除配置
     * @param Request $request
     * @return SelfResponse|\Illuminate\Http\JsonResponse
     */
    public function batchDelete(Request $request)
    {
        $ids = $request->get('id');
        if(!$ids){
            return SelfResponse::error('缺少必要的操作参数');
        }
        return SelfResponse::error('不支持删除');
    }

}